package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;

    @Autowired
    private IStatementService statementService;

    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }

    @Override
    public void save(Appointment appointment) {
        appointmentMapper.insert(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(Appointment appointment) {
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public void deleteBatch(Long id, Integer statusCancel) {
        //软删除
        Appointment appointment = this.get(id);
        if (Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
            appointmentMapper.deleteStatus(id, Appointment.DELETE_YES);
        } else if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
            throw new BusinessException("不能删除");
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    @Override
    public void cancelStatus(Long id, Integer statusCancel) {
        //点击取消状态从预约中变成用户取消
        Appointment appointment = this.get(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
            throw new BusinessException("不是预约状态不能取消");
        }
        appointmentMapper.setcancel(id, Appointment.STATUS_CANCEL);
    }

    @Override
    public void arrival(Long id, Integer statusArrival) {
        //点击到店 状态从预约中变成到店 到店时间 当前时间
        Appointment appointment = this.get(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
            throw new BusinessException("不是预约状态不能到店");
        }
        appointmentMapper.arrival(id, Appointment.STATUS_ARRIVAL, new Date());
    }

    @Override
    @Transactional
    public Long generateStatement(Long appointmentId) {
        //通过前台传入的id获取服务结算单的信息并封装成对象
        Statement statement = statementService.generateStatement(appointmentId);
        //判断是否有服务结算单的信息 如果是空则把信息预约的数据设置到statement对象中
        if (statement == null) {
            //获取信息预约单的信息并封装成对象
            Appointment appointment = appointmentMapper.selectByPrimaryKey(appointmentId);
            //
            statement = new Statement();
            //设置创建时间
            statement.setCreateTime(new Date());
            //实际到店时间
            statement.setActualArrivalTime(appointment.getActualArrivalTime());
            //汽车类型
            statement.setCarSeries(appointment.getCarSeries());
            //客户名称
            statement.setCustomerName(appointment.getCustomerName());
            //车牌号码
            statement.setLicensePlate(appointment.getLicensePlate());
            //预约单ID
            statement.setAppointmentId(appointment.getId());
            //服务类型
            statement.setServiceType(appointment.getServiceType().longValue());
            //用户联系方式
            statement.setCustomerPhone(appointment.getCustomerPhone());
            //保存
            statementService.save(statement);
            //修改预约单状态
            appointmentMapper.changStatus(appointmentId, Appointment.STATUS_SETTLE);
        }
        return statement.getId();
    }
}